﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLibrary.API.Security;

namespace FifaChamps.WebUI.Security
{
    public class LoginManager
    {
        #region SingleTon
        private static LoginManager m_LoginManager;
        #endregion

        #region Constructors

        private LoginManager() : this("FIFACHAMPS") { }
        private LoginManager(string passcode)
        {
            m_EncryptionTool = new RijndaelEnhanced(passcode);
            m_Tokens = new List<LoginToken>();
        }

        #endregion

        #region Properties

        private RijndaelEnhanced m_EncryptionTool = new RijndaelEnhanced("FIFACHAMPS");
        private List<LoginToken> m_Tokens = new List<LoginToken>();
        public const string SUCCESS_URL = "default.aspx";
        public const string FAILURE_URL = "login.aspx";
        const string TOKEN_CONTEXT_KEY = "TOKEN";
        const string TOKEN_COOKIE_KEY = "TOKEN_COOKIE";
        const string TOKEN_COOKIE_USERNAME = "TOKEN_USERNAME";
        const string TOKEN_COOKIE_PASSWORD = "TOKEN_PASSWORD";
        #endregion

        #region Methods


        public LoginToken Get_LoggedIn()
        {
            try
            {
                //check the context for the current user
                var token_session = HttpContext.Current.Session[TOKEN_CONTEXT_KEY];
                if (token_session != null) return (LoginToken)token_session;

                var cookie_key = HttpContext.Current.Request.Cookies[TOKEN_COOKIE_KEY];
                if (cookie_key != null)
                {
                    var username = cookie_key.Values[TOKEN_COOKIE_USERNAME];
                    var password = cookie_key.Values[TOKEN_COOKIE_PASSWORD];

                    if (!string.IsNullOrEmpty(password)) password = m_EncryptionTool.Decrypt(password);

                    return Login(username, password);
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        public LoginToken Login(string username, string password)
        {
            LoginToken token = LoginToken.Factory(username, password);
            var key = token.Authenticate();

            if (key != Guid.Empty)
            {
                //setup session
                HttpContext.Current.Session.Add(TOKEN_CONTEXT_KEY, token);
                //setup cookie
                HttpCookie cookie = new HttpCookie(TOKEN_COOKIE_KEY);
                cookie.Expires = DateTime.Now.AddDays(3);
                cookie.Values[TOKEN_COOKIE_USERNAME] = username;
                cookie.Values[TOKEN_COOKIE_PASSWORD] = m_EncryptionTool.Encrypt(password);
                HttpContext.Current.Response.Cookies.Add(cookie);

                return token;
            }
            else
            {
                return null;
            }
        }

        public void Logout(string username) { }

        private void Store(LoginToken token) { }

        public static LoginManager Get()
        {
            if (m_LoginManager == null)
            {
                m_LoginManager = new LoginManager();
            }

            return m_LoginManager;
        }

        #endregion
    }
}